//
//  RAPStatusCellViewModel.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 1/1/16.
//  Copyright © 2016 RendezvousAuParadis. All rights reserved.
//

import UIKit

// TODO: 点击，评论，赞的万级数量显示
class RAPStatusCellViewModel: NSObject {
    
    var statusModel: RAPStatusModel
//        didSet {
//            // 来源字符串
//            if var string = statusModel.source {
//                string = string.substringFromIndex((string.rangeOfString("\">")?.endIndex)!)
//                string = string.substringToIndex((string.rangeOfString("</")?.startIndex)!)
//                self.source_Text = "来自 \(string)"
//            }
//            
//        }
//    }

    // TODO: 数量的万级显示
    var mbRank_ImageName: String? {
        if let rank = statusModel.user?.mbrank {
            
            if rank > 0 && rank < 7 {
                return "common_icon_membership_level\(rank)"
            }
        }
        return nil
    }

    // TODO: 认证图标有复用bug
    var verified_type_ImageName: String? {
        
        if let vp = statusModel.user?.verified_type {
            switch vp {
            case -1: // 什么都没有
                return nil
            case 0: // 大v
                return "avatar_vip"
            case 1: // 草根
                return "avatar_grassroot"
            case 2: // 女孩
                return "avatar_vgirl"
            case 3: // 企业
                return "avatar_enterprise_vip"
            default:
                return nil
            }
        }
        return nil
    }
    
    // TODO: 待删除
    var tweetMessage: String? {
        let regex = "(@\\w+)|(#\\w+#)|(http(s)?://([A-Za-z0-9.?_-]+(/)?)*)";
        let arr = (statusModel.text! as NSString).componentsMatchedByRegex(regex);

        
        return arr.description
    }
    
    
    private func getAttributedString(text: String?) {
        guard let opText = text else {
            return
        }
        
        (opText as NSString).enumerateStringsMatchedByRegex("") { (count, stringPtr, rangePtr, _) -> Void in
            print("count:\(count)--->stringPtr:\(stringPtr)--->range:\(rangePtr)")
        }
        
    }
    

    var retweetMessage: String? {
        if let status = statusModel.retweeted_status {
            return "@\(status.user!.screen_name!)：\(status.text!)"
        }
        return nil
    }
    
    /**
    来源字符串转换
    */
    var source_Text: String? {
//        if let status = statusModel {
//            "<a href="http://weibo.com" rel="nofollow">新浪微博</a>"
        if var string = statusModel.source where string != "" {
            string = string.substringFromIndex((string.rangeOfString("\">")?.endIndex)!)
            string = string.substringToIndex((string.rangeOfString("</")?.startIndex)!)
            return "来自 \(string)"
        }
        
        return ""
    }
    
    /**
    时间字符串转换
    */
    var created_at_Text: String? {
        if let opCreated_at = statusModel.created_at {
            
            // 获取到日期对象
            // 如果date不存在，就返回
            let sinaDate = NSDate.convertSINADateString(opCreated_at)
            
            guard let opSinaDate = sinaDate else {
                return nil
            }
            return opSinaDate.SINADateDescription()
        }
        return nil
    }
    
    
    var statusPictureModelArr: [RAPStatusPictureModel]?
    
    /// 图片是否是gif
    lazy var isOriPicGIFs: [Bool] = [Bool]()
    
    lazy var isRetPicGIFs:[Bool] = [Bool]()
    
    
    // MARK: - 首页图文混排
    
    

    // MARK: - 系统方法
    init(statusModel: RAPStatusModel) {
        self.statusModel = statusModel
        super.init()
        
        /*
        判断配图来自哪里
        */
        if let pua = statusModel.pic_urls where pua.count > 0 { // 配图来自原创微博
            statusPictureModelArr = pua
        } else if let pua = statusModel.retweeted_status?.pic_urls where pua.count > 0 { // 配图来自转发微博
            statusPictureModelArr = pua
        } // 除此之外，statusPictureModelArr为nil
        
        // 原创微博配图的gif判断
        guard let opOriPic_urls = statusModel.pic_urls else {
            return
        }
        for model in opOriPic_urls {
            print(model.thumbnail_pic)
            let pathExtension = (model.thumbnail_pic! as NSString).pathExtension
            if pathExtension == "gif" {
                isOriPicGIFs.append(true)
            } else {
                isOriPicGIFs.append(false)
            }
        }
        
        // 转发微博gif的判断
        guard let opRetPic_urls = statusModel.retweeted_status?.pic_urls else {
            return
        }
        for model in opRetPic_urls {
            print(model.thumbnail_pic)
            let pathExtension = (model.thumbnail_pic! as NSString).pathExtension
            //            print(pathExtension)
            if pathExtension == "gif" {
                isRetPicGIFs.append(true)
            } else {
                isRetPicGIFs.append(false)
            }
        }
        
    }
    
    
    override var description: String {
        let descDict = dictionaryWithValuesForKeys(["statusModel"])
        return descDict.description
    }
    
}
